//
//  MessageViewController.m
//  XiangMuB
//
//  Created by 999999999999 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MessageViewController.h"

#import "SenderButton.h"
#import <pop/POP.h>
#import "SendTextViewController.h"

@interface MessageViewController ()<UITabBarControllerDelegate>

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    
    self.tabBarController.delegate = self;
    
    
    [self setupAnimationViews];
    
    
}

- (void)setupAnimationViews{
    
    [self setupSubviews];
    
    NSArray *imageArray = @[@"2.jpg",@"3.jpg",@"4.jpg"];
    NSArray *titleArray = @[@"发图片",@"发段子",@"发视频"];
    
    int maxCols = 3;
    CGFloat buttonW = 72;
    CGFloat buttonH = buttonW + 30;
    CGFloat buttonStartX = 20;
    CGFloat buttonStartY = ([UIScreen mainScreen].bounds.size.height - buttonH) / 2;
    CGFloat buttonMargin = ([UIScreen mainScreen].bounds.size.width - 2 * buttonStartX - 3 * buttonW) / 2;
    
    for (int i = 0; i < imageArray.count; i++) {
        
        SenderButton *button = [[SenderButton alloc] init];
        
        button.tag = i;
        [button setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        
        [button addTarget:self action:@selector(sendMessages:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:button];
        
        int row = i / maxCols;
        int col = i % maxCols;
        
        CGFloat buttonX = buttonStartX + col * (buttonMargin + buttonW);
        CGFloat buttonY = buttonStartY + row * buttonH;
        CGFloat buttonBeginY = buttonY - [UIScreen mainScreen].bounds.size.height;
        
        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        animation.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        animation.springBounciness = 10;
        animation.springSpeed = 10;
        animation.beginTime = CACurrentMediaTime() + 0.1 * i;
        
        [button pop_addAnimation:animation forKey:nil];
        
        
        
    }

    
    
    
}


#pragma mark private method

- (void)sendMessages:(SenderButton *)button{
    
    
    
    
    [self cancelWithCompletionBlock:^{
       
        if (button.tag == 0) {
            
            
            [self pushViewControllerWith:button.tag];

            
        } else if (button.tag == 1) {
            
            
            [self pushViewControllerWith:button.tag];
            
        } else if (button.tag == 2) {
            
            [self pushViewControllerWith:button.tag];
            
        }
        
        
        
    }];
    
    
    
}

- (void)pushViewControllerWith:(NSInteger)tagButton{
    
    SendTextViewController *sendText = [[SendTextViewController alloc] init];
    
    sendText.buttonTag = tagButton;
    
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:sendText];
    
    UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
    [root presentViewController:navigation animated:YES completion:nil];
    
}

- (void)cancelWithCompletionBlock:(void(^)())completionBlock{
    
    
    int beginIndex = 0;
    
    for (int i = beginIndex; i < self.view.subviews.count; i++) {
        
        UIView *subview = self.view.subviews[i];
        
        POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        
        CGFloat centerY = subview.center.y + [UIScreen mainScreen].bounds.size.height;
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(subview.center.x, centerY)];
        animation.beginTime = CACurrentMediaTime() + (i - beginIndex) * 0.1;
        [subview pop_addAnimation:animation forKey:nil];
        
        if (i == self.view.subviews.count - 1) {
            
            [animation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
               
                [self dismissViewControllerAnimated:NO completion:nil];
                
                !completionBlock ? :completionBlock();
                
            }];
            
        }
        
    }
    
}

- (void)setupSubviews{
    
    CGFloat buttonH = self.tabBarController.tabBar.frame.size.height + 50;
    
    UIButton *button = [[UIButton alloc] init];
    [button setFrame:CGRectMake(0, self.view.frame.size.height - buttonH, self.view.frame.size.width, 50)];
    [button setBackgroundColor:[UIColor orangeColor]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle:@"取消" forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(cancelButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    
    
}


- (void)cancelButton{
    
    [self cancelWithCompletionBlock:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self cancelWithCompletionBlock:nil];
}





#pragma mark UITabbar delegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    if ([viewController.title isEqualToString:@"新帖"]) {
        
        [self setupAnimationViews];
        NSLog(@"%s",__func__);

        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
